
package linuxbox.admin.bean;

import java.sql.*;
import java.util.*;
import java.sql.Date;
import linuxbox.exception.*;

import linuxbox.BoxStatic;
import linuxbox.util.*;
import linuxbox.admin.logic.*;
import linuxbox.bean.*;
import linuxbox.admin.exception.*;

public class UserCtrlBean extends TableBean {
    private int m_iID;
    private String m_sLoginID;
    private String m_sFirstName;
    private String m_sLastName;
    private String m_sPassword;
    private String m_sUserRights;
    private String m_sUserRights2;
    private Date m_dtBeginDate;
    private Date m_dtEndDate;
    private String m_sAddress;
    private String m_sCity;
    private String m_sState;
    private String m_sZipCode;
    private String m_sPhone;
    private String m_sFax;
    private String m_sMobile;
    private String m_sEmail;
    private int m_iManagerID;
    private int m_iDeptID;
    private String m_sDeptName;
    private int m_bIsManager;
    private int m_bDisable;
    private int m_bNoExpire;
    private String m_sNote;
    
    public UserCtrlBean(UserCtrlKey pk, DbAdapter db) {
        this.init(pk, db);
    }
    
    public UserCtrlBean(UserCtrlKey pk) {
        this.init(pk, null);
    }

    private void init(UserCtrlKey pk, DbAdapter db) {
        super.init(db);
        if (pk != null)
            m_iID = pk.m_iID;
        super.setTableName("usercontrol");
    }

    protected void load() throws UserCtrlBeanException {
        //BoxStatic.logger.debug("I am entering load() in UserCtrlBean");

        if (!m_bLoaded) {
            try {
                String sql;
                makeConnection();
                sql = "select u.loginid as loginid, u.firstname as firstname, "
                    + "u.lastname as lastname, "
                    + "u.password as password, u.userrights as userrights, "
                    + "u.userrights2 as userrights2, u.address as address, u.city as city, "
                    + "u.state as state, u.zipcode as zipcode, "   
                    + "u.phone as phone, u.fax as fax, u.mobile as mobile, u.email as email, "
                    + "u.dept_id as dept_id, d.name as name, u.managerid as managerid, "
                    + "u.is_manager as is_manager, u.disable as disable, u.noexpire as noexpire, "
                    + "u.note as note from usercontrol u, department d where u.id = " 
                    + m_iID + " and u.dept_id=d.id";
                //BoxStatic.logger.debug(sql);
                m_db.executeQuery(sql);
                
                if (m_db.next()) {
                    m_sLoginID = m_db.getString("loginid");
                    m_sFirstName = m_db.getString("firstname");
                    m_sLastName = m_db.getString("lastname");
                    m_sPassword = m_db.getString("password");
                    m_sUserRights = m_db.getString("userrights");
                    m_sUserRights2 = m_db.getString("userrights2");
                    m_sAddress = m_db.getString("address");
                    m_sCity = m_db.getString("city");
                    m_sState = m_db.getString("state");
                    m_sZipCode = m_db.getString("zipcode");
                    m_sPhone = m_db.getString("phone");
                    m_sFax = m_db.getString("fax");
                    m_sMobile = m_db.getString("mobile");
                    m_sEmail = m_db.getString("email");
                    m_iDeptID = m_db.getInt("dept_id");
                    m_sDeptName = m_db.getString("name");
                    m_iManagerID = m_db.getInt("managerid");
                    m_bIsManager = m_db.getInt("is_manager");
                    m_bDisable = m_db.getInt("disable");
                    m_bNoExpire = m_db.getInt("noexpire");
                    m_sNote = m_db.getString("note");
                } else {
                    throw new UserCtrlBeanException("Load error: Police(" 
                        + m_iID + ") not found!");
                }
                m_bLoaded = true;
                close();
            } catch(DbBeanException e1) {
                throw new UserCtrlBeanException("Can't load the Police record: " 
                    + e1.getMessage());
            } catch(TableBeanException e2) {
                throw new UserCtrlBeanException("Can't load the Police record: " 
                    + e2.getMessage());
            }
        }
        //BoxStatic.logger.debug("I am leaving load() in UserCtrlBean");
    }

    public void store() throws UserCtrlBeanException {
        try {
            String where = " id = " + m_iID;
            super.updateTable(where);
        } catch(TableBeanException e2) {
            throw new UserCtrlBeanException("Can't update in store: " + e2.getMessage());
        }
    }
    
    public void refresh() throws UserCtrlBeanException {
        m_bLoaded = false;
        load();
    }

    protected UserCtrlKey create(User user) throws UserCtrlBeanException {
        try {
            BoxStatic.logger.debug("I am entering create(user) in UserCtrlBean");
            String sql = "select max(id) from usercontrol";
            makeConnection();
            m_db.executeQuery(sql);
            
            if (m_db.next()) {
                int maxID = m_db.getInt(1);
                m_iID = maxID + 1;
            }
                        
            String userrights = Long.toHexString(user.getUserRights());
            String userrights2 = Long.toHexString(user.getUserRights2());
            
            String noexpire = "0";
            if (user.getNoExpire())
                noexpire = "1";
                
            String disable = "0";
            if (user.getDisable())
                disable = "1";

            String isManager = "0";
            if (user.getIsManager())
                isManager = "1";

            sql = "insert into usercontrol (id, loginid, firstname, "
                + "lastname, password, userrights, userrights2, address, "
                + "city, state, zipcode, phone, fax, mobile, email, dept_id, "
                + "managerid, is_manager, disable, noexpire, note) "
                + "values (" + m_iID + ", '" + user.getLoginID() + "', '" 
                + user.getFirstName() + "', '" + user.getLastName() + "', '" 
                + user.getPassword() + "', '" + userrights + "', '" + userrights2 
                + "', '" + user.getAddress() + "', '" + user.getCity() + "', '"
                + user.getState() + "', '" + user.getZipCode() + "', '" 
                + user.getPhone() + "', '" + user.getFax() + "', '" + user.getMobile()
                + "', '" + user.getEmail() + "', " + user.getDeptID() + ", " 
                + user.getManagerID() + ", " + isManager + ", " 
                + disable + ", " + noexpire + ", '" + user.getNote() + "')"; 
            
            //BoxStatic.logger.debug(sql);
            m_db.executeUpdate(sql);
            close();
        
            BoxStatic.logger.debug("I am leaving create(user) in UserCtrlBean");
            return new UserCtrlKey(m_iID);
        } catch(Exception e) {
            throw new UserCtrlBeanException(e.getMessage());
        }
    }

    protected void create(UserCtrlKey key) throws UserCtrlBeanException {
        try {
            String sql = "insert into usercontrol (id, loginid, firstname, "
                + "lastname, password, userrights, userrights2, noexpire) "
                + "values (" + m_iID + ", 'none', 'none', 'none', 'none', '0', '0', 0)"; 
            //BoxStatic.logger.debug("create sql =" + sql);
            makeConnection();
            m_db.executeUpdate(sql);
            close();
        } catch(DbBeanException e1) {
            throw new UserCtrlBeanException("Can't create the Police record: " 
                + e1.getMessage());
        } catch(TableBeanException e2) {
            throw new UserCtrlBeanException("Can't create the Police record: " 
                + e2.getMessage());
        }
    }

    protected void remove() throws UserCtrlBeanException {
        try {
            String sql = "delete usercontrol where id = " + m_iID;
            //BoxStatic.logger.debug("remove sql =" + sql);
            makeConnection();
            m_db.executeUpdate(sql);
            close();
        } catch(DbBeanException e1) {
            throw new UserCtrlBeanException("Can't delete the Police record: " 
                + e1.getMessage());
        } catch(TableBeanException e2) {
            throw new UserCtrlBeanException("Can't delete the Police record: " 
                + e2.getMessage());
        }
    }
    
    protected UserCtrlKey findByKey() throws UserCtrlBeanException {
        UserCtrlKey pk = null;
        try {
            String sql = "select id from usercontrol where id = " + m_iID;
            //BoxStatic.logger.debug("findByKey sql = " + sql);
            makeConnection();
            m_db.executeQuery(sql);

            if (m_db.next()) {
                pk = new UserCtrlKey();
                pk.setID(m_db.getInt(1));
            }
            
            if (m_db.next()) {
                throw new UserCtrlBeanException("more that one user found");
            }
            
            close();
        } catch(DbBeanException e1) {
            throw new UserCtrlBeanException("Can't find the Police record: " 
                + e1.getMessage());
        } catch(TableBeanException e2) {
            throw new UserCtrlBeanException("Can't find the Police record: " 
                + e2.getMessage());
        }

        return pk;
    }

    protected UserCtrlKey findByLoginID(String loginID) throws UserCtrlBeanException {
        UserCtrlKey pk = null;
        try {
            String sql = "select id from usercontrol where loginid = '" 
                + loginID + "'";
            //BoxStatic.logger.debug("findByLoginID sql = " + sql);
            makeConnection();
            m_db.executeQuery(sql);

            if (m_db.next()) {
                pk = new UserCtrlKey();
                pk.setID(m_db.getInt(1));
            }
            
            if (m_db.next()) {
                throw new UserCtrlBeanException("more that one user found");
            }
            
            close();
        } catch(DbBeanException e1) {
            throw new UserCtrlBeanException("Can't find the Police record: " 
                + e1.getMessage());
        } catch(TableBeanException e2) {
            throw new UserCtrlBeanException("Can't find the Police record: " 
                + e2.getMessage());
        }

        return pk;
    }

    protected List findBySql(String sqlWhere) throws UserCtrlBeanException {
        List v = null;
        try {
            String sql = "select id from usercontrol " + sqlWhere;
            //BoxStatic.logger.debug(sql);
            makeConnection();
            m_db.executeQuery(sql);
            UserCtrlKey pk = null;
            v = new ArrayList();
            while (m_db.next()) {
                pk = new UserCtrlKey();
                pk.setID(m_db.getInt(1));
                v.add(pk);
            }
            close();
        } catch(DbBeanException e1) {
            throw new UserCtrlBeanException("Can't find the Police record: " 
                + e1.getMessage());
        } catch(TableBeanException e2) {
            throw new UserCtrlBeanException("Can't find the Police record: " 
                + e2.getMessage());
        }
        return v;
    }

    public int getID() {
        return m_iID;
    }

    public String getLoginID() throws UserCtrlBeanException {
        load();
        return m_sLoginID;
    }

    public void setLoginID(String str) {
        if (str != null) {
            m_sLoginID = str;
            setString("loginid", str);
        }
    }

    public String getPassword() throws UserCtrlBeanException {
        load();
        return m_sPassword;
    }
    
    public void setPassword(String str) throws UserCtrlBeanException {
        if (str != null) {
            m_sPassword = str;
            setString("password", str);
        }
    }
    
    public String getFirstName() throws UserCtrlBeanException {
        load();
        return m_sFirstName;
    }
    
    public void setFirstName(String str) throws UserCtrlBeanException {
        if (str != null) {
            m_sFirstName = str;
            setString("firstname", str);
        }
    }
    
    public String getLastName() throws UserCtrlBeanException {
        load();
        return m_sLastName;
    }
    
    public void setLastName(String str) throws UserCtrlBeanException {
        if (str != null) {
            m_sLastName = str;
            setString("lastname", str);
        }
    }

    public long getUserRights() throws UserCtrlBeanException {
        load();
        long l = -1L;
        
        try {
            l = Long.parseLong(m_sUserRights, 16);
        } catch (NumberFormatException e) {
            throw new UserCtrlBeanException(e.getMessage());
        }
        
        return l;
    }
    
    public void setUserRights(long rights) throws UserCtrlBeanException {
        String str = Long.toHexString(rights);
        
        if (str != null) {
            m_sUserRights = str;
            setString("userrights", str);
        }
    }

    public long getUserRights2() throws UserCtrlBeanException {
        load();
        long l = -1L;
        
        try {
            l = Long.parseLong(m_sUserRights2, 16);
        } catch (NumberFormatException e) {
            throw new UserCtrlBeanException(e.getMessage());
        }
        
        return l;
    }
    
    public void setUserRights2(long rights) throws UserCtrlBeanException {
        String str = Long.toHexString(rights);
        
        if (str != null) {
            m_sUserRights2 = str;
            setString("userrights", str);
        }
    }

    public Date getBeginDate() throws UserCtrlBeanException {
        load();
        return m_dtBeginDate;
    }
    
    public void setBeginDate(Date dt, Time tm) throws UserCtrlBeanException {
        if (dt != null) {
            m_dtBeginDate = dt;
            if (tm == null)
                setDate("begindate", dt, new Time(0L));
            else
                setDate("begindate", dt, tm);
        }
    }

    public Date getEndDate() throws UserCtrlBeanException {
        load();
        return m_dtEndDate;
    }
    
    public void setEndDate(Date dt, Time tm) throws UserCtrlBeanException {
        if (dt != null) {
            m_dtEndDate = dt;
            if (tm == null)
                setDate("enddate", dt, new Time(0L));
            else
                setDate("enddate", dt, tm);
        }
    }

    public String getAddress() throws UserCtrlBeanException {
        load();
        return m_sAddress;
    }
    
    public void setAddress(String address) throws UserCtrlBeanException {
        if (address != null) {
            m_sAddress = address;
            setString("address", address);
        }
    }
    
    public String getCity() throws UserCtrlBeanException {
        load();
        return m_sCity;
    }
    
    public void setCity(String city) throws UserCtrlBeanException {
        if (city != null) {
            m_sCity = city;
            setString("city", city);
        }
    }
    
    public String getState() throws UserCtrlBeanException {
        load();
        return m_sState;
    }
    
    public void setState(String state) throws UserCtrlBeanException {
        if (state != null) {
            m_sState = state;
            setString("state", state);
        }
    }
        
    public String getZipCode() throws UserCtrlBeanException {
        load();
        return m_sZipCode;
    }
    
    public void setZipCode(String zip) throws UserCtrlBeanException {
        if (zip != null) {
            m_sZipCode = zip;
            setString("zipcode", zip);
        }
    }
    
    public String getPhone() throws UserCtrlBeanException {
        load();
        return m_sPhone;
    }   
    
    public void setPhone(String phone) throws UserCtrlBeanException {
        if (phone != null) {
            m_sPhone = phone;
            setString("phone", phone);
        }
    }
    
    public String getFax() throws UserCtrlBeanException {
        load();
        return m_sFax;
    }
    
    public void setFax(String fax) throws UserCtrlBeanException {
        if (fax != null) {
            m_sFax = fax;
            setString("fax", fax);
        }
    }
    
    public String getMobile() throws UserCtrlBeanException {
        load();
        return m_sMobile;
    }
    
    public void setMobile(String mobile) throws UserCtrlBeanException {
        if (mobile != null) {
            m_sMobile = mobile;
            setString("mobile", mobile);
        }
    }
    
    public String getEmail() throws UserCtrlBeanException {
        load();
        return m_sEmail;
    }
    
    public void setEmail(String email) throws UserCtrlBeanException {
        if (email != null) {
            m_sEmail = email;
            setString("email", email);
        }
    }
    
    public int getManagerID() throws UserCtrlBeanException {
        load();
        return m_iManagerID;
    }
    
    public void setManagerID(int id) throws UserCtrlBeanException {
        m_iManagerID = id;
        setInteger("managerid", id);
    }
    
    public int getDeptID() throws UserCtrlBeanException {
        load();
        return m_iDeptID;
    }
    
    public void setDeptID(int id) throws UserCtrlBeanException {
        m_iDeptID = id;
        setInteger("dept_id", id);
    }
    
    public String getDeptName() throws UserCtrlBeanException {
        load();
        return m_sDeptName;
    }
    
    public boolean getIsManager() throws UserCtrlBeanException {
        load();
        if(m_bIsManager == 1)
            return true;
        else
            return false;
    }
    
    public void setIsManager(boolean isManager) throws UserCtrlBeanException {
        if (isManager == true)
            m_bIsManager = 1;
        else
            m_bIsManager = 0;
            
        setInteger("is_manager", m_bIsManager);
    }

    public boolean getDisable() throws UserCtrlBeanException {
        load();
        if(m_bDisable == 1)
            return true;
        else
            return false;
    }
    
    public void setDisable(boolean dis) throws UserCtrlBeanException {
        if (dis == true)
            m_bDisable = 1;
        else
            m_bDisable = 0;
            
        setInteger("disable", m_bDisable);
    }

    public boolean getNoExpire() throws UserCtrlBeanException {
        load();
        if(m_bNoExpire == 1)
            return true;
        else
            return false;
    }
    
    public void setNoExpire(boolean noe) throws UserCtrlBeanException {
        if (noe == true)
            m_bNoExpire = 1;
        else
            m_bNoExpire = 0;
            
        setInteger("noexpire", m_bNoExpire);
    }

    public String getNote() throws UserCtrlBeanException {
        load();
        return m_sNote;
    }
    
    public void setNote(String str) throws UserCtrlBeanException {
        if (str != null) {
            m_sNote = str;
            setString("note", str);
        }
    }
}